package com.jzhao.jpre.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.jzhao.jpre.base.StringPublicDeal;
import com.jzhao.jpre.model.AnsCode;

public class AnsCodeDao {
	protected JdbcTemplate jdbcTemplate;
	protected StringPublicDeal strPubDeal;

	class AnsCodeRowMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			AnsCode ansCode = new AnsCode();
			ansCode.setMrchCode(strPubDeal.trim(rs.getString("mrchCode")));
			ansCode.setAnsCode(strPubDeal.trim(rs.getString("ansCode")));
			ansCode.setSysErrCode(strPubDeal.trim(rs.getString("sysErrCode")));
			ansCode.setAnsExpCn(strPubDeal.trim(strPubDeal.convToGB2312(rs
					.getString("ansExpCn"))));
			ansCode.setAnsExpEn(strPubDeal.trim(rs.getString("ansExpEn")));

			return ansCode;
		}
	}

	public void setDataSource(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public void setStrPubDeal(StringPublicDeal strPubDeal) {
		this.strPubDeal = strPubDeal;
	}

	public AnsCode SelectByKey(String mrchCode, String code)
			throws DataAccessException {
		AnsCode ansCode = null;
		String sql = "SELECT * FROM t_js_ans_code WHERE mrchCode='"
				+ mrchCode.trim() + "' AND ansCode='" + code.trim() + "'";
		ansCode = (AnsCode) jdbcTemplate.queryForObject(sql,
				new AnsCodeRowMapper());
		return ansCode;
	}
}
